package com.egao.customer.controller;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.*;
import com.egao.common.core.annotation.OperLog;
import com.egao.customer.entity.Customer;
import com.egao.customer.entity.PayHistory;
import com.egao.customer.service.CustomerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.ui.Model;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.text.ParseException;


/**
 *
 * 客户管理
 * @author : Doman
 * @date : 2020-06-13 13:36:04
 * @version ：1.0
 */

@Controller
@RequestMapping("/customer/customer")
public class CustomerController extends BaseController {

	@Autowired
	private CustomerService customerService;
	
	/**
	 * 页面
	 **/
	@RequiresPermissions("customer:customer:view")
	@RequestMapping()
	public String view() {
		return "customer/customer.html";
	}

	/**
	 * 分页查询列表
	 */
	@RequiresPermissions("customer:customer:list")
	@OperLog(value = "客户管理", desc = "分页查询")
	@ResponseBody
	@RequestMapping("/page")
    public JSONObject list(PageResult2<T> page, Customer customer) throws Exception{
        return customerService.list(page, customer, getLoginUser());
    }

    /**
     * 查询全部客户管理
     */
    @RequiresPermissions("customer:customer:list")
    @OperLog(value = "客户管理", desc = "查询全部")
    @ResponseBody
    @RequestMapping("/list")
    public String list(Customer customer){
        return JSONArray.fromObject(customerService.list(customer, getLoginUser())).toString();
    }

    /**
     * 根据id查询客户管理
     */
    @RequiresPermissions("customer:customer:list")
    @OperLog(value = "客户管理", desc = "根据id查询")
    @ResponseBody
    @RequestMapping("/get")
    public JsonResult get(Customer customer) {
        return JsonResult.ok().setData(customerService.getById(customer));
		// 使用关联查询
        //PageParam<customer> pageParam = new PageParam<>();
		//pageParam.put("id", id);
        //List<customer> records = customerService.listAll(pageParam.getNoPageParam());
        //return JsonResult.ok().setData(pageParam.getOne(records));
    }

    /**
     * 客户违规
     * 这个不用了
     */
    /*@RequiresPermissions("customer:customer:save")
    @OperLog(value = "客户违规", desc = "违规", param = false, result = true)
    @ResponseBody
    @RequestMapping("/violations")*/
    public JsonResult violations(Model model, Customer customer, PayHistory ph) {
        return customerService.violations(customer, ph, getLoginUser());
    }

    /**
     * 修改卡片id
     */
    @RequiresPermissions("customer:customer:save")
    @OperLog(value = "卡片id修改", desc = "卡片id修改", param = false, result = true)
    @ResponseBody
    @RequestMapping("/cardUpdate")
    public JsonResult cardUpdate(Model model, Customer customer, PayHistory payHistory)  {
        return customerService.cardUpdate(customer, payHistory, getLoginUser());
    }

    /**
     * 报停
     */
    @RequiresPermissions("customer:customer:save")
    @OperLog(value = "客户缴费", desc = "缴费", param = false, result = true)
    @ResponseBody
    @RequestMapping("/jf")
    public JsonResult jf(Model model, Customer customer, PayHistory payHistory) throws ParseException {
        try {
            return customerService.jf(customer, payHistory, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error("缴费失败");
        }
    }

    /**
     * 退款
     */
    @RequiresPermissions("customer:customer:save")
    @OperLog(value = "退款", desc = "退款", param = false, result = true)
    @ResponseBody
    @RequestMapping("/tk")
    public JsonResult tk(Model model, Customer customer, PayHistory payHistory) {
        return customerService.tk(customer, payHistory, getLoginUser());
    }

    /**
     * 开口费
     */
    @RequiresPermissions("customer:customer:save")
    @OperLog(value = "客户开口费", desc = "开口费", param = false, result = true)
    @ResponseBody
    @RequestMapping("/kkf")
    public JsonResult kkf(Model model, Customer customer, PayHistory payHistory) {
        return customerService.kkf(customer, payHistory, getLoginUser());
    }

    /**
     * 预付款
     */
    @RequiresPermissions("customer:customer:save")
    @OperLog(value = "客户预付款", desc = "预付款", param = false, result = true)
    @ResponseBody
    @RequestMapping("/yfk")
    public JsonResult yfk(Model model, Customer customer, PayHistory payHistory) {
        return customerService.yfk(customer, payHistory, getLoginUser());
    }

    /**
     * 报停
     */
    @RequiresPermissions("customer:customer:save")
    @OperLog(value = "客户报停", desc = "报停", param = false, result = true)
    @ResponseBody
    @RequestMapping("/tb")
    public JsonResult tb(Model model, Customer customer) {
        return customerService.bt(customer, getLoginUser());
    }

    /**
     * 添加客户管理
     */
    @RequiresPermissions("customer:customer:save")
    @OperLog(value = "客户管理", desc = "添加", param = false, result = true)
    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(Model model, Customer customer) {
        return customerService.save(customer, getLoginUser());
    }

    /**
     * 修改客户管理
     */
    @RequiresPermissions("customer:customer:update")
    @OperLog(value = "客户管理", desc = "修改", param = false, result = true)
    @ResponseBody
    @RequestMapping("/update")
    public JsonResult update(Model model, Customer customer) {
        return customerService.update(customer, getLoginUser());
    }

    /**
     * 删除客户管理
     */
    @RequiresPermissions("customer:customer:remove")
    @OperLog(value = "客户管理", desc = "删除", result = true)
    @ResponseBody
    @RequestMapping("/remove")
    public JsonResult remove(Customer customer ) {
        return customerService.remove(customer, getLoginUser());
    }

    /**
     * 批量添加客户管理
     */
    /**@RequiresPermissions("customer:customer:save")
    @OperLog(value = "客户管理", desc = "批量添加", param = false, result = true)
    @ResponseBody
    @RequestMapping("/saveBatch")
    public JsonResult saveBatch(@RequestBody List<Customer> list) {
        if (customerService.saveBatch(list)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }*/

    /**
     * 批量修改客户管理
     */
    /**@RequiresPermissions("customer:customer:update")
    @OperLog(value = "客户管理", desc = "批量修改", result = true)
    @ResponseBody
    @RequestMapping("/updateBatch")
    public JsonResult updateBatch(@RequestBody BatchParam<Customer> batchParam) {
        if (batchParam.update(customerService, "")) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }*/

//    *
//     * 批量删除客户管理
//
//    *@RequiresPermissions("customer:customer:remove")
//    @OperLog(value = "客户管理", desc = "批量删除", result = true)
//    @ResponseBody
//    @RequestMapping("/removeBatch")
//    public JsonResult removeBatch(@RequestBody List<Integer> ids) {
//        if (customerService.removeByIds(ids)) {
//            return JsonResult.ok("删除成功");
//        }
//        return JsonResult.error("删除失败");
//    }

}
